#include "stdafx.h"
#include "AudioSampler.h"
#include <windows.h>	// Used for PlaySound().
#include <Mmsystem.h>

#pragma comment(lib,"Winmm.lib")

void AudioSampler::playTone()
{
	// Play one metronome tone.
	PlaySound("tick.wav", NULL, SND_ASYNC|SND_FILENAME);
}

bool AudioSampler::prepareTuner()
{
	if(!rstream_.open(init_))
	{
		return false; //if rstream is not successfully opened, return false
	}
	return true;
}

void AudioSampler::stopTuner()
{
	rstream_.close();
}

float AudioSampler::getFrequency()
{
	bool gotFreq = false;
	Pa_Sleep(100); //sleep for enough time to get audio
		
	float max, avg_abs, bestpeak_freq;
	
	//get the frequency information
	gotFreq = rstream_.audioInput(max, avg_abs, bestpeak_freq);
		
	//if a frequency is gotten and the max amplitude is greater 0.25, return the frequency
	if(gotFreq && max > 0.15 && avg_abs > 0.05)
	{
		return bestpeak_freq;
	}
	else
	{
		return 0; //otherwise, just return 0
	}
}